import pandas as pd
from calc_manager.base.plugins import PluginObject
import requests
from io import StringIO


class MatCSVReader(PluginObject):
    def __init__(self, oss_url, local_path=None) -> None:
        if local_path:
            csv_data = local_path
        else:
            response = requests.get(oss_url)
            response.raise_for_status()
            csv_data = StringIO(response.text)

        self.df = pd.read_csv(csv_data)

    def get_mat_from_csv(self, mid):
        # 示例材料的csv数据第一列是材料id,第二列是需要读取的值
        for index, row in self.df.iterrows():
            if row.iloc[0] == mid:
                return row.iloc[1]